import Vue from 'vue'
import store from '../store'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'
import shoppingRoute from './modules/shopping'
import memberRoute from './modules/member'
import userRoute from './modules/user'
import cardRoute from './modules/card'
import pollsRoute from './modules/polls'
// import { getToken } from '@/utils/auth'

Vue.use(VueRouter)

const routes = [
  {
    path: '/auth',
    name: 'auth',
    meta: {
      title: '用户授权'
    },
    component: () => import('@/components/auth/Auth')
  },
  {
    path: '/',
    name: 'Home',
    component: Home,
    meta: {
      title: '商品详情',
      keepAlive: true,
    }
  },

  ...shoppingRoute,
  ...memberRoute,
  ...userRoute,
  ...cardRoute,
  ...pollsRoute,
  
  {
    path: '/error',
    name: 'error',
    meta: {
      title: '错误提示'
    },
    component: () => import('@/components/Error')
  },
  { path: '*', redirect: '/404' }
]

const router = new VueRouter({
  routes
})


// 挂载路由导航守卫
router.beforeEach((to, from, next) => {
  document.title = to.matched[0].meta.title
  const openid = localStorage.getItem('openid');
  // next()
  if (!openid) {
    if (to.path === '/auth') {
      next()
    } else {
      localStorage.setItem('to_url', to.fullPath)
      next('/auth')
    }
  } else {
    //获取用户是否为商家标识
    // console.log(store.state.retailer_id)
    // console.log(store.state.retailer_name)
    // console.log(store.state.retailer_code)
    // console.log(store.state.employee)
   
      const hasWxStaff = store.getters.wx_staff
      if (hasWxStaff == -1) {
        store.dispatch('getUserInfo').then(() => {
        }).catch(error => {
          console.log(error.messge);
        })
      }


    //
    if (store.state.retailer_id == -1) {
      store.dispatch('getRetailerInfo').then(() => {

      }).catch(error => {
        console.log(error.messge);
      })
    }
    next()
  }

})

export default router
